package com.joysuch.wwyt.edu.entity;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.joysuch.wwyt.edu.enums.CertificateNotifyObjType;
import com.joysuch.wwyt.edu.enums.CertificateNotifyStatus;
import com.joysuch.wwyt.edu.enums.CertificateNotifyType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 证书提醒对象 wwyt_edu_certificate_notify
 *
 * @author eric
 * @date 2021-04-16
 */
@ApiModel("证书提醒")
@Data
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@Accessors(chain = true)
@TableName("wwyt_edu_certificate_notify")
public class EduCertificateNotify implements Serializable {

private static final long serialVersionUID=1L;


    /** $column.columnComment */
    @ApiModelProperty("id")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /** $column.columnComment */
    private Long tenentId;

    /** $column.columnComment */
    private String orgCode;

    /** 提醒名称 */
    @ApiModelProperty("提醒名称")
    private String name;

    /**
     * {@link com.joysuch.wwyt.edu.enums.CertificateNotifyType}
     */
    /** 0-有效期到期提醒/1-复证到期提醒 */
    @ApiModelProperty(value = "提醒类型", allowableValues = "0,1", notes = "0-有效期到期提醒/1-复证到期提醒")
    private Integer type;

    /** 提前天数 */
    @ApiModelProperty(value = "提前天数", notes = "1-365")
    private Integer aheadDays;

    /**
     * {@link com.joysuch.wwyt.edu.enums.CertificateNotifyObjType}
     */
    /** 提醒对象类型: 0-人员/1-部门负责人/2-管理类型 */
    @ApiModelProperty(value = "提醒对象类型", allowableValues = "0,1,2", notes = "0-人员/1-部门负责人/2-管理类型")
    private Integer objType;

    /** 提醒对象id */
    @ApiModelProperty(value = "提醒对象id")
    @TableField(value = "obj_id",fill = FieldFill.INSERT_UPDATE)
    private String objId;

    /** 提醒证书类型ids  json字符串 */
    @ApiModelProperty(value = "提醒证书类型ids, 多个用英文逗号隔开")
    private String certificates;

    /** 是否提醒本身 1-是/0-否 */
    @ApiModelProperty(value = "是否提醒本身")
    private Boolean notifySelfFlag;

    /**
     * {@link com.joysuch.wwyt.edu.enums.CertificateNotifyStatus}
     */
    /** 提醒启用状态 1-开启/0-关闭 */
    @ApiModelProperty(value = "提醒启用状态 1-开启/0-关闭")
    private Integer status;

    /** $column.columnComment */
    private Long createBy;

    /** $column.columnComment */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /** $column.columnComment */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    /** $column.columnComment */
    private Long updateBy;

    private transient List<Long> certIdList;

    public void validate(){

        if(ObjectUtil.isNull(tenentId)){
            throw new RuntimeException("缺少tenentId");
        }

        if(StrUtil.isEmpty(orgCode)){
            throw new RuntimeException("缺少orgCode");
        }

        if(StrUtil.isEmpty(name)){
            throw new RuntimeException("提醒名称必填");
        }

        if(ObjectUtil.isNull(type)){
            throw new RuntimeException("提醒类型必填");
        }
        if(ObjectUtil.isNull(CertificateNotifyType.findById(type))){
            throw new RuntimeException("提醒类型数据非法");
        }

        if(ObjectUtil.isNull(aheadDays)){
            throw new RuntimeException("提醒提前量必填");
        }
        if(aheadDays < 1 || aheadDays > 365){
            throw new RuntimeException("提醒提前量为1-365之间的正整数");
        }

        if(ObjectUtil.isNull(objType)){
            throw new RuntimeException("提醒对象类型必填");
        }
        if(ObjectUtil.isNull(CertificateNotifyObjType.findById(objType))){
            throw new RuntimeException("提醒对象类型数据非法");
        }

        if(ObjectUtil.isNull(notifySelfFlag)){
            notifySelfFlag = true;
        }

        if(ObjectUtil.isNull(status)){
            status = CertificateNotifyStatus.KAI_QI.getId();
        }

//        if(StrUtil.isEmpty(certificates)){
//            throw new RuntimeException("未选择相关证书种类");
//        }


    }

}
